﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;

public partial class Administration : System.Web.UI.Page
{
    public string tabId = "0";

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                getSomSettings();
                getProwiseSettings();
            }
        }
        catch (Exception ex)
        {
            OmniaLogger.Log.Error("Getting SOM settings form web.config failed: ", ex);
        }
    }

    private void getSomSettings()
    {
        if ((somBrinNr.Text = ConfigurationManager.AppSettings["SomBrinnummer"]) == null)
            OmniaLogger.Log.Error("Getting SomBrinnummer failed, check your web.config file");
        if ((somBeveiligingsCode.Text = ConfigurationManager.AppSettings["SomBeveiligingscode"]) == null)
            OmniaLogger.Log.Error("Getting SomBeveiligingscode failed, check your web.config file");
        if ((somLoginUrl.Text = ConfigurationManager.AppSettings["SomLoginUrl"]) == null)
            OmniaLogger.Log.Error("Getting SomLoginUrl fialed, check your web.config file");
        if ((somWebserviceEndpoint.Text = ConfigurationManager.AppSettings["SomWebserviceEndpoint"]) == null)
            OmniaLogger.Log.Error("Getting SomWebserviceEndpoint failed, check your web.config file");
    }

    private void getProwiseSettings()
    {
        if ((ProwiseBrinNr.Text = ConfigurationManager.AppSettings["ProwiseBrinnummer"]) == null)
            OmniaLogger.Log.Error("Getting ProwiseBrinnummer failed, check you web.config file");
        if ((ProwiseOrgId.Text = ConfigurationManager.AppSettings["ProwiseOrgId"]) == null)
            OmniaLogger.Log.Error("Getting ProwiseOrgId failed, check you web.config file");
        if ((ProwiseSharedSecret.Text = ConfigurationManager.AppSettings["ProwiseBeveiligingscode"]) == null)
            OmniaLogger.Log.Error("Getting ProwiseSharedSecret failed, check you web.config file");
    }


    protected void saveSom_Click(object sender, EventArgs e)
    {
        tabId = "0";
        try
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
            config.AppSettings.Settings["SomBrinnummer"].Value = somBrinNr.Text.ToString();
            config.AppSettings.Settings["SomBeveiligingscode"].Value = somBeveiligingsCode.Text.ToString();
            config.AppSettings.Settings["SomLoginUrl"].Value = somLoginUrl.Text.ToString();
            config.AppSettings.Settings["SomWebserviceEndpoint"].Value = somWebserviceEndpoint.Text.ToString();
            config.Save(ConfigurationSaveMode.Modified);
            Label1.Visible = Label1.Visible ? false : true;
        }
        catch (Exception ex)
        {
            OmniaLogger.Log.Error("Setting Som Single Sign On settings failed ", ex);
        }
    }

    protected void saveProwise_Click(object sender, EventArgs e)

    {
        tabId = "1";
        try
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("/");
            config.AppSettings.Settings["ProwiseBrinnummer"].Value = ProwiseBrinNr.Text.ToString();
            config.AppSettings.Settings["ProwiseBeveiligingscode"].Value = ProwiseSharedSecret.Text.ToString();
            config.AppSettings.Settings["ProwiseOrgId"].Value = ProwiseOrgId.Text.ToString();
            config.Save(ConfigurationSaveMode.Modified);
            Label1.Visible = Label1.Visible ? false : true;
        }
        catch (Exception ex)
        {
            OmniaLogger.Log.Error("Setting Prowise Single Sign On Settings failed ", ex);
        }
    }
}